package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DzDeptsyncMapper;
import com.ruoyi.system.domain.DzDeptsync;
import com.ruoyi.system.service.IDzDeptsyncService;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 组织同步Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-31
 */
@Service
public class DzDeptsyncServiceImpl implements IDzDeptsyncService 
{
    @Autowired
    private DzDeptsyncMapper dzDeptsyncMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询组织同步
     * 
     * @param zzId 组织同步主键
     * @return 组织同步
     */
    @Override
    public DzDeptsync selectDzDeptsyncByZzId(Long zzId)
    {
        return dzDeptsyncMapper.selectDzDeptsyncByZzId(zzId);
    }

    @Override
    public DzDeptsync selectDzDeptsyncByCode(String unitCode) {
        return dzDeptsyncMapper.selectDzDeptsyncByCode(unitCode);
    }

    /**
     * 查询组织同步列表
     * 
     * @param dzDeptsync 组织同步
     * @return 组织同步
     */
    @Override
    public List<DzDeptsync> selectDzDeptsyncList(DzDeptsync dzDeptsync)
    {

        //权限控制
        SysUser user=  SecurityUtils.getLoginUser().getUser();
        List<SysRole> listRole= sysRoleService.selectRolesByUserId(user.getUserId());
        int datascope = 5;
        for(SysRole role:listRole){
            if( role.getDataScope()!=null&&Integer.parseInt(role.getDataScope())<datascope){
                datascope=Integer.parseInt(role.getDataScope());
            }
        }
        dzDeptsync.setDatascope(datascope);
        if(datascope==4){
            dzDeptsync.setCheckDeptId(user.getDeptId());
        }
        if(datascope==5){
            DzHuiyuan huiyuan =  dzHuiyuanService.selectDzHuiyuanByDn(user.getPhonenumber());
            if(huiyuan!=null){
                dzDeptsync.setCheckHuiyuanId(huiyuan.getId());
            }else{
                dzDeptsync.setCheckHuiyuanId(0l);
            }

        }
        return dzDeptsyncMapper.selectDzDeptsyncList(dzDeptsync);
    }

    @Override
    public List<DzDeptsync> selectDzDeptsyncListWeb(DzDeptsync dzDeptsync)
    {
        return dzDeptsyncMapper.selectDzDeptsyncList(dzDeptsync);
    }

    /**
     * 新增组织同步
     * 
     * @param dzDeptsync 组织同步
     * @return 结果
     */
    @Override
    public int insertDzDeptsync(DzDeptsync dzDeptsync)
    {
        return dzDeptsyncMapper.insertDzDeptsync(dzDeptsync);
    }

    /**
     * 修改组织同步
     * 
     * @param dzDeptsync 组织同步
     * @return 结果
     */
    @Override
    public int updateDzDeptsync(DzDeptsync dzDeptsync)
    {
        return dzDeptsyncMapper.updateDzDeptsync(dzDeptsync);
    }

    /**
     * 批量删除组织同步
     * 
     * @param zzIds 需要删除的组织同步主键
     * @return 结果
     */
    @Override
    public int deleteDzDeptsyncByZzIds(Long[] zzIds)
    {
        return dzDeptsyncMapper.deleteDzDeptsyncByZzIds(zzIds);
    }

    /**
     * 删除组织同步信息
     * 
     * @param zzId 组织同步主键
     * @return 结果
     */
    @Override
    public int deleteDzDeptsyncByZzId(Long zzId)
    {
        return dzDeptsyncMapper.deleteDzDeptsyncByZzId(zzId);
    }
}
